import { NextResponse } from "next/server"

export async function POST(req: Request) {
  try {
    const { prompt } = await req.json()

    // 构建提示词
    const enhancedPrompt = `Generate a photorealistic image of a cocktail: ${prompt}. The image should be high quality, well-lit, with professional styling and garnishing. Include glassware, ice, and appropriate garnishes. Style: commercial beverage photography.`

    // 创建请求配置
    const payload = {
      model: process.env.IMAGE_MODEL || "dall-e-3",
      prompt: enhancedPrompt,
      n: 1,
      size: "1024x1024",
      quality: "hd",
      style: "vivid",
    }

    // 发送请求到API
    const response = await fetch(`${process.env.API_BASE_URL}/images/generations`, {
      headers: {
        "Content-Type": "application/json",
        Authorization: `Bearer ${process.env.API_KEY}`,
      },
      method: "POST",
      body: JSON.stringify(payload),
    })

    if (!response.ok) {
      const errorData = await response.json()
      throw new Error(errorData.error?.message || `HTTP error! status: ${response.status}`)
    }

    const data = await response.json()

    return NextResponse.json(
      {
        url: data.data[0].url,
        created: new Date().toISOString(),
      },
      { status: 200 }
    )
  } catch (error) {
    console.error("Image Generation API Error:", error)
    return NextResponse.json(
      {
        error: error instanceof Error ? error.message : "An unknown error occurred",
        timestamp: new Date().toISOString(),
        requestId: Math.random().toString(36).substring(7),
      },
      { status: 500 }
    )
  }
} 